<template>
  <a-modal
    :title='title'
    :width='1280'
    :visible='visible'
    :confirmLoading='confirmLoading'
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'>

    <a-spin :spinning='confirmLoading'>
      <a-form-model ref='form' v-bind='layout' :model='model' :rules='validatorRules'>
        <a-row>
          <a-col :span='12'>
            <a-form-model-item label='栏目名称'  >
              <a-input  v-model='model.columnName'  disabled='disabled'/>
            </a-form-model-item>
          </a-col>
          <a-col :span='12'>
            <a-form-model-item label='封面图' required prop='image'>
              <j-image-upload class='avatar-uploader' text='上传' v-model='model.image' biz-path='img'></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span='12'>
            <a-form-model-item label='标题' required prop='title'>
              <a-input placeholder='请输入标题' v-model='model.title' />
            </a-form-model-item>
          </a-col>
          <a-col :span='12'>
            <a-form-model-item label='关键字' required prop='keywords'>
              <a-input placeholder='请输入关键字' v-model='model.keywords' />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row justify='start'>
          <a-form-model-item label='简单描述' required prop='description' :label-col='{span: 2 }' :wrapperCol='{span: 20 }'
                             style='margin-left: 26px'>
            <a-input placeholder='请输入描述' v-model='model.description' />
          </a-form-model-item>
        </a-row>
        <a-row justify='start'>
          <a-form-model-item label='资讯内容' required prop='content' :label-col='{span: 2 }' :wrapperCol='{span: 20 }'
                             style='margin-left: 26px'>
            <j-editor v-model='model.content' style='height: 500px'></j-editor>
          </a-form-model-item>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, putAction, getAction, postAction } from '@api/manage'
import JEditor from '@comp/jeecg/JEditor'

export default {
  name: 'hCmsArticleModal',
  components: {
    JEditor
  },
  data() {
    return {
      title: '操作',
      visible: false,
      model: {
        columnCode: ''
      },
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 19 }
      },
      confirmLoading: false,
      validatorRules: {
        columnCode: [{ required: true, message: '请输入栏目编码!' }],
        title: [{ required: true, message: '请输入内容标题!' }],
        keywords: [{ required: true, message: '请输入关键字!' }],
        description: [{ required: true, message: '请输入描述!' }],
        content: [{ required: true, message: '请输入文章内容!' }],
        image: [{ required: true, message: '请上传封面图片!' }],
      },
      url: {
        get: '/h/cms/article',
        add: '/h/cms/article',
        edit: '/h/cms/article',
        column: '/h/cms/column/queryTreeList',
        getColumn: '/h/cms/column',
      },
      columnList: []
    }
  },
  created() {
    const that = this
    that.modelDefault = JSON.parse(JSON.stringify(that.model))
    getAction(this.url.column).then((res) => {
      if (res.success) {
        that.columnList = res.result
      }
    })
  },
  methods: {
    add(columnId) {
      this.model = Object.assign({}, {})
      this.visible = true
      getAction(this.url.getColumn+"/"+columnId).then((res)=>{
        this.$set(this.model, 'columnCode', res.result.columnCode);
        this.$set(this.model, 'columnName', res.result.columnName);
      });
    },
    edit(record) {
      const that = this
      that.model = Object.assign({}, record)
      that.visible = true
      getAction(that.url.get + '/' + record.id).then((res) => {
        if (res.success) {
          that.model = res.result
        }
      })
    },
    close() {
      this.$refs.form.clearValidate()
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
              that.close()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }


  }
}
</script>

<style scoped>

</style>